﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TreeViewLearn
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //this.treeView1.ShowLines = false;
            //this.treeView1.ShowPlusMinus = false;
            //this.treeView1.ShowRootLines = false;
            //this.treeView1.HideSelection = false;
            this.treeView1.Font = new Font(Font.FontFamily, 16);
            treeView1.LabelEdit = true;//可编辑状态。  
      
            TreeNode node = new TreeNode();
            node.Text = "hope";
            treeView1.Nodes.Add(node);
            TreeNode node1 = new TreeNode();
            node1.Text = "hopeone";
            TreeNode node11 = new TreeNode();
            node11.Text = "hopeoneone";
            TreeNode node2 = new TreeNode();
            node2.Text = "hopetwo";
            node1.Nodes.Add(node11);//在node1下面在添加一个结点。  
            node.Nodes.Add(node1);//node下的两个子节点。  
            node.Nodes.Add(node2);

            TreeNode t = new TreeNode("basil");//作为根节点。  
            treeView1.Nodes.Add(t);
            TreeNode t1 = new TreeNode("basilone");
            t.Nodes.Add(t1);
            TreeNode t2 = new TreeNode("basiltwo");
            t.Nodes.Add(t2);
            this.treeView1.ImageList = this.imageList1;
        }

        private void treeView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Point ClickPoint = new Point(e.X, e.Y);
                int x = e.X;
                int y = e.Y;
                TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint);
                if (CurrentNode is TreeNode)//判断你点的是不是一个节点
                {
                    treeView1.SelectedNode = CurrentNode;
                    CurrentNode.ContextMenuStrip = this.contextMenuStrip1;
                    contextMenuStrip1.Show(MousePosition);
                }
                else
                {
                    treeView1.ContextMenuStrip = this.contextMenuStrip2;
                    contextMenuStrip2.Show(MousePosition);
                }
            }
        }

        private void 添加子节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 f5 = new Form4();
            if (f5.ShowDialog() == DialogResult.OK)
            {
                TreeNode node = new TreeNode(f5.NodeName, 0, 1);
                treeView1.SelectedNode.Nodes.Add(node);
            }
        }

        private void 删除选中节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Remove();
        }

        private void 添加根节点ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 f4 = new Form4();
            if (f4.ShowDialog() == DialogResult.OK)
            {
                treeView1.Nodes.Add(f4.NodeName);
            }
        }

        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.Nodes.Clear();
        }

        private void 修改名称ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form4 f5 = new Form4(treeView1.SelectedNode.Text);
            if (f5.ShowDialog() == DialogResult.OK)
            {
                treeView1.SelectedNode.Text = f5.NodeName;
            }
        }
    }
}
